/* eslint-disable no-unused-vars */
import Feature from 'ol/Feature';
import { Circle as CircleStyle, Stroke, Style, Fill, Text, RegularShape } from 'ol/style';
import Polygon from 'ol/geom/Polygon';
import VectorSource from 'ol/source/Vector';
import VectorLayer from 'ol/layer/Vector';
import GeoJSON from 'ol/format/GeoJSON';

import { pointLonToXy, CalculateContour, CalculateSelect, transformLonlatToNow } from "./util"
import { shipStyleList, shipPointStyleList, LayerZIndex } from "../options"

// 三角形基础信息
const setSanFeature = (row, type = 1) => {
  let shipFeature = new Feature({
    geometry: pointLonToXy(row.lon, row.lat),
    name: row.shipName || "",
    mmsi: row.mmsi,
    heading: row.course,
    shipType: type,
    sty: 'sp',
    detail: row
  });
  shipFeature.setId("san" + row.targetNum);
  shipFeature.setStyle(setSanStyle(row, type))
  return shipFeature
}
// 三角形样式
const setSanStyle = (row, type = 1) => {
  return new Style({
    image: new RegularShape({
      // fill: new Fill({ color: "#00ff00" }),
      stroke: new Stroke({
        // color: row.isSelect ? "#f00" : "#0be037",
        // color: row.arpaType !== 1 ? "#00ff00" : "#D18616",
        color: "#00ff00",
        width: 2.5,
        miterLimit: 1
      }),
      radius: 6,
      points: 3,
      rotation: row.course === (0 || undefined) ? 0 : Math.PI / (180 / row.course),
      scale: [1, 2]
    }),
  })
}
// AIS
const setAISSanFeature = (row, type = 1) => {
  let shipFeature = new Feature({
    geometry: pointLonToXy(row.lon, row.lat),
    name: row.shipName || "",
    mmsi: row.mmsi,
    heading: row.course,
    shipType: type,
    sty: 'sp',
    detail: row
  });
  shipFeature.setId("san" + row.targetNum);
  shipFeature.setStyle(setAISSanStyle(row, type))
  return shipFeature
}
// AIS样式
const setAISSanStyle = (row, type = 1) => {
  return new Style({
    image: new RegularShape({
      fill: new Fill({ color: "#00ff00" }),
      stroke: new Stroke({
        // color: row.isSelect ? "#f00" : "#0be037",
        // color: row.arpaType !== 1 ? "#00ff00" : "#D18616",
        color: "#000",
        width: 1,
        miterLimit: 1
      }),
      radius: 6,
      points: 3,
      rotation: row.course === (0 || undefined) ? 0 : Math.PI / (180 / row.course),
      scale: [1, 2]
    }),
  })
}
// 圆形基础信息
const setYuanFeature = (row, type = 1) => {
  let shipFeature = new Feature({
    geometry: pointLonToXy(row.lon, row.lat),
    name: row.shipName || "",
    mmsi: row.mmsi,
    heading: row.course,
    shipType: type,
    sty: 'sp',
    detail: row
  });
  shipFeature.setId(row.targetNum);
  shipFeature.setStyle(setYuanStyle(row, type))
  return shipFeature
}
// 圆形样式
const setYuanStyle = (row, type = 1) => {
  let text = row.shipName || ""
  return new Style({
    image: new CircleStyle({
      radius: 13,
      stroke: new Stroke({
        color: row.arpaType === "0" ? "#f00" : 'rgba(0, 255, 0, 1)',
        width: row.arpaType === "0" ? 3.5 : 2.5,
      }),
      fill: new Fill({
        color: 'rgba(255, 255, 255, 0)'
      })
    }),
    // text: new Text({
    //   textAlign: 'center',
    //   text: text,
    //   offsetX: 30,
    //   offsetY: -15,
    //   fill: new Fill({
    //     color: "#0be037"
    //   }),
    //   // backgroundFill: new Stroke({
    //   //   color: "rgba(255,255,255,0.2)",
    //   //   width: 2.5,
    //   // }),
    // })
  })
}
// 艏向线样式
const setHeadingStyle = (row) => {
  return new Style({
    image: new RegularShape({
      fill: new Fill({ color: "#000" }),
      stroke: new Stroke({
        color: 'rgba(0,0,0,0)',
        width: 1.25,
      }),
      radius: 2,
      points: 6,
      rotation: Math.PI / (180 / row.heading),
      scale: [0.4, 4],
      displacement: [0, 5]
    }),
    stroke: new Stroke({
      color: '#f00',
      width: 2
    }),
  })
}
// 艏向线
const setShipHeadingLine = (row) => {
  let headingFeature = new Feature({
    mmsi: `head-${row.mmsi}`,
    geometry: pointLonToXy(row.longitude, row.latitude),
  });
  headingFeature.setId(`head-${row.mmsi}`);
  headingFeature.setStyle(setHeadingStyle(row))
  return headingFeature
}
/**
 * 绘制选中框
 * @param {*} list [经度, 纬度]
 * @param {*} type 选中框类型，base:三角和圆点，real:真轮廓
 */
const shipCheckBoxId = "ship_check_box"
const drawCheckBox = (row, type = "base", map) => {
  const list = [row.longitude, row.latitude]
  drawCheckBoxByRadius(list, map)
}
// 点、圆下的选中框
const drawCheckBoxByRadius = (list, map) => {
  // 边长
  var radius = 20
  var layerTmp = map.getLayers().getArray().find(m => m.get("name") === "RADIUS")
  if (!layerTmp) {
    // 添加图层
    layerTmp = new VectorLayer({
      source: new VectorSource({
        features: [],
      }),
      zIndex: LayerZIndex.radius,
      name: "RADIUS",
      type: "base"
    });
    map.addLayer(layerTmp)
  }
  let shipCheckBoxSource = null
  if (layerTmp.get("type") !== "base") {
    shipCheckBoxSource = new VectorSource({
      features: [],
    })
    layerTmp.values_.type = "base"
  } else {
    shipCheckBoxSource = layerTmp.getSource()
  }
  var CheckBoxFeature = shipCheckBoxSource.getFeatureById(shipCheckBoxId);
  if (CheckBoxFeature === null) {
    var longRadius = radius * Math.SQRT2;
    let styless = new Style({
      image: new RegularShape({
        stroke: new Stroke({
          color: "#f00",
          width: 0.8,
          lineDash: [longRadius * 3 / 10, longRadius * 4 / 10, longRadius * 3 / 10, 0]
        }),
        radius: radius,
        points: 4,
        rotation: Math.PI / (180 / 45),
      }),
    })
    let shipFeature = new Feature({
      geometry: pointLonToXy(list[0], list[1]),
    });
    shipFeature.setId(shipCheckBoxId);
    shipFeature.setStyle(styless)
    shipCheckBoxSource.addFeature(shipFeature);
  } else {
    CheckBoxFeature.setGeometry(pointLonToXy(list[0], list[1]));
  }
  layerTmp.setSource(shipCheckBoxSource)
}

const yuanType = (row, shipSource, type = 1) => {
  var newshipFeature = shipSource.getFeatureById(row.targetNum);
  if (newshipFeature === null) {
    shipSource.addFeature(setYuanFeature(row, type));
  } else {
    row = Object.assign({}, newshipFeature.values_.detail, row)
    newshipFeature.setGeometry(pointLonToXy(row.lon, row.lat));
    newshipFeature.values_.detail = row;
  }
}
const sanType = (row, shipSource, type = 1) => {
  var sanShipFeature = shipSource.getFeatureById("san" + row.targetNum);
  // 三角
  if (sanShipFeature === null) {
    shipSource.addFeature(setSanFeature(row, type));
  } else {
    sanShipFeature.setGeometry(pointLonToXy(row.lon, row.lat));
    sanShipFeature.values_.detail = row;
  }
}
const sanAisType = (row, shipSource, type = 1) => {
  var sanShipFeature = shipSource.getFeatureById("san" + row.targetNum);
  if (sanShipFeature === null) {
    shipSource.addFeature(setAISSanFeature(row, type));
  } else {
    sanShipFeature.setGeometry(pointLonToXy(row.lon, row.lat));
    sanShipFeature.values_.detail = row;
  }
}

export default {
  // 基本船型
  drawBaseShip: (row, type, shipSource, map) => {
    if (!row) return
    // yuanType(row, shipSource)
    if (row.arpaType === "0") {
      yuanType(row, shipSource)
    } else if (row.arpaType === "1") {
      sanAisType(row, shipSource)
    } else if (row.arpaType === "2") {
      sanType(row, shipSource)
      // sanAisType(row, shipSource)
      yuanType(row, shipSource)
    }
  },
}
